import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from "../views/Reg/Reg.vue"
import Login from "../views/Login/Login.vue"
import Main from "../views/Main/Main.vue"
import store from '@/store'
import Home from "../views/menus/Home/Home.vue"
import UserInfo from "../views/menus/User/UserInfo.vue"
import UserAvatar from "../views/menus/User/UserAvatar.vue"
import UserPwd from '../views/menus/User/UserPwd.vue'
import ArtCate from '../views/menus/Article/ArtCate.vue'
import ArtList from '../views/menus/Article/ArtList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: Home,
      },
      {
        path: 'user-info',
        component: UserInfo
      },
      {
        path: 'user-avatar',
        component: UserAvatar
      },
      {
        path: 'user-pwd',
        component: UserPwd
      },
      {
        path: 'art-cate',
        component: ArtCate
      },
      {
        path: 'art-list',
        component: ArtList
      }
    ]
  },
]

const router = new VueRouter({
  routes,
  // mode: 'history' //去掉路径里的#号
})

router.beforeEach((to, from, next) => {
  const token = store.state.token
  // if (to.path !== '/reg' || to.path !== '/login') {
  //   next()
  // } else {
  //   if (!token) {
  //     next('/login')
  //   } else {
  //     next()
  //   }
  // }
  if (!token && to.path !== '/reg' && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})


export default router
